angular.service(ꞌutilꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 32

Size

Total Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
nc 32
dl 0
loc 111
rs 8.2857
c 3
b 1
f 0
nop 2

10 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 10 4
B ��) 0 15 6
A ��) 0 7 2
A ��) 0 3 1
A ��) 0 13 4
A ��) 0 5 1
B ��) 0 17 5
A ��) 0 9 3
A ��) 0 12 3
A ��) 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* globals numberformat,window */
2
/**
3
 util
4
 Utility service with misc. functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('util', ['$sce',
13
    'data',
14
    function($sce, data) {
15
      let sv = this;
16
      /* Return the HTML representation of an element, or the element itself
17
      if it doesn't have one */
18
      sv.getHTML = function(resource) {
19
        let html = data.html[resource];
20
        if (typeof html === 'undefined' && data.resources[resource]) {
21
          html = data.resources[resource].html;
22
        }
23
        if (typeof html === 'undefined') {
24
          return resource;
25
        }
26
        return html;
27
      };
28
29
      sv.prettifyNumber = function(number, player) {
30
        if (typeof number === 'undefined' || number === null){
31
           return null;
32
        }
33
        if (number === ''){
34
           return '';
35
         }
36
        if (number === Infinity){
37
           return '∞';
38
         }
39
        if (number === 0){
40
           return '0';
41
         }
42
        return numberformat.format(number, player.options.numberformat);
43
      };
44
45
      sv.addResource = function(player, scope, key, quantity, state){
46
        if(quantity === 0){
47
          return;
48
        }
49
        sv.addStatistic(player, scope, key, quantity);
50
        if (player.resources[key] === null) {
51
          player.resources[key] = quantity;
52
          state.addNew(key);
53
        }else{
54
          player.resources[key] += quantity;
55
        }
56
      };
57
58
      /* Adds an statistic. Scope can be only all time, dark run,
59
        only for specific elements, or for all elements at once
60
      */
61
      sv.addStatistic = function(player, scope, key, value){
62
        setStatistic(player.statistics.all_time, key, value);
63
        if(scope === 'all_time'){
64
          return;
65
        }
66
        setStatistic(player.statistics.dark_run, key, value);
67
        if(scope === 'dark'){
68
          return;
69
        }
70
        if(scope === 'all_elements') {
71
          scope = Object.keys(data.elements);
72
        }
73
        for(let element of scope){
74
          player.statistics.exotic_run[element] = player.statistics.exotic_run[element] || {};
75
          setStatistic(player.statistics.exotic_run[element], key, value);
76
        }
77
      };
78
79
      function setStatistic(bucket, key, value){
80
        // If it is numeric, add it up
81
        if(!isNaN(parseFloat(value)) && isFinite(value)){
82
          bucket[key] = bucket[key]+value || value;
83
        // otherwise, replace
84
        }else{
85
          bucket[key] = value;
86
        }
87
      }
88
89
      sv.trustHTML = function(html) {
90
        return $sce.trustAsHtml(html);
91
      };
92
93
      sv.nextAmount = function (player, index, array) {
94
        player.options[index] = (player.options[index] + 1) % array.length;
95
      };
96
97
      sv.delayedExec = function(currentTs, eventTs, delay, callback) {
98
        if(currentTs-eventTs >= delay){
99
          callback();
100
        }else{
101
          window.requestAnimationFrame((ts) => sv.delayedExec(ts, eventTs, delay, callback));
102
        }
103
      };
104
105
      sv.prestigeProduction = function(number, start, power){
106
        number = number || 0;
107
        let production = Math.pow(Math.E,(-0.5+Math.sqrt(0.25+0.8686*Math.log(number/start)))/(2/Math.log(power*power))) || 0;
108
        return Math.round(Math.max(0, production));
109
      };
110
111
      sv.calculateValue = function(number, value, level){
112
        let result = number;
113
        if(value.linear){
114
           result *= level*value.linear;
115
         }
116
        if(value.poly){
117
           result *= Math.floor(Math.pow(level, value.poly));
118
         }
119
        if(value.exp){
120
           result *= Math.floor(Math.pow(value.exp, level));
121
         }
122
        return result;
123
      }
124
    }
125
  ]);
126